/**
 * @file ecu_filter.c
 * @brief 汽车ECU滤波算法模块实现
 */

#include "filter/ecu_filter.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define FILTER_MAGIC_NUMBER 0xDEADBEEF
#define MAX_FILTER_ORDER 64

/**
 * @brief 滤波器内部结构体
 */
typedef struct {
    uint32_t magic;         /**< 魔数用于验证句柄有效性 */
    filter_config_t config; /**< 滤波器配置 */
    float* state_buffer;    /**< 状态缓冲区 */
    uint16_t state_index;   /**< 当前状态索引 */
    uint32_t sample_count;  /**< 采样计数 */
    float sum;              /**< 当前和（用于移动平均） */
} filter_internal_t;

filter_handle_t* filter_create(const filter_config_t* config) {
    if (config == NULL || config->order == 0 || config->order > MAX_FILTER_ORDER) {
        return NULL;
    }
    
    // 分配滤波器句柄内存
    filter_internal_t* filter = (filter_internal_t*)malloc(sizeof(filter_internal_t));
    if (filter == NULL) {
        return NULL;
    }
    
    // 初始化基本信息
    filter->magic = FILTER_MAGIC_NUMBER;
    memcpy(&filter->config, config, sizeof(filter_config_t));
    filter->state_index = 0;
    filter->sample_count = 0;
    filter->sum = 0.0f;
    
    // 分配状态缓冲区
    filter->state_buffer = (float*)calloc(config->order, sizeof(float));
    if (filter->state_buffer == NULL) {
        free(filter);
        return NULL;
    }
    
    return (filter_handle_t*)filter;
}

void filter_destroy(filter_handle_t* handle) {
    if (handle == NULL) {
        return;
    }
    
    filter_internal_t* filter = (filter_internal_t*)handle;
    
    // 验证魔数
    if (filter->magic != FILTER_MAGIC_NUMBER) {
        return;
    }
    
    // 释放资源
    if (filter->state_buffer != NULL) {
        free(filter->state_buffer);
    }
    
    free(filter);
}

float filter_update(filter_handle_t* handle, float input) {
    if (handle == NULL) {
        return 0.0f;
    }
    
    filter_internal_t* filter = (filter_internal_t*)handle;
    
    // 验证魔数
    if (filter->magic != FILTER_MAGIC_NUMBER) {
        return 0.0f;
    }
    
    float output = 0.0f;
    
    switch (filter->config.type) {
        case FILTER_TYPE_MAV: {
            // 移动平均滤波实现
            if (filter->sample_count < filter->config.order) {
                filter->sum += input;
                filter->sample_count++;
                output = filter->sum / filter->sample_count;
            } else {
                filter->sum -= filter->state_buffer[filter->state_index];
                filter->sum += input;
                output = filter->sum / filter->config.order;
            }
            break;
        }
        
        case FILTER_TYPE_IIR: {
            // IIR滤波实现
            if (filter->config.coefficients != NULL) {
                // 简化的IIR实现
                output = filter->config.coefficients[0] * input;
                for (uint16_t i = 1; i < filter->config.order; i++) {
                    output += filter->config.coefficients[i] * 
                             filter->state_buffer[(filter->state_index + i) % filter->config.order];
                }
            }
            break;
        }
        
        default:
            output = input;
            break;
    }
    
    // 更新状态缓冲区
    filter->state_buffer[filter->state_index] = input;
    filter->state_index = (filter->state_index + 1) % filter->config.order;
    
    return output;
}

void filter_reset(filter_handle_t* handle) {
    if (handle == NULL) {
        return;
    }
    
    filter_internal_t* filter = (filter_internal_t*)handle;
    
    if (filter->magic == FILTER_MAGIC_NUMBER && filter->state_buffer != NULL) {
        memset(filter->state_buffer, 0, filter->config.order * sizeof(float));
        filter->state_index = 0;
        filter->sample_count = 0;
        filter->sum = 0.0f;
    }
}

filter_type_t filter_get_type(const filter_handle_t* handle) {
    if (handle == NULL) {
        return FILTER_TYPE_MAV;
    }
    
    const filter_internal_t* filter = (const filter_internal_t*)handle;
    return (filter->magic == FILTER_MAGIC_NUMBER) ? filter->config.type : FILTER_TYPE_MAV;
}

uint16_t filter_get_order(const filter_handle_t* handle) {
    if (handle == NULL) {
        return 0;
    }
    
    const filter_internal_t* filter = (const filter_internal_t*)handle;
    return (filter->magic == FILTER_MAGIC_NUMBER) ? filter->config.order : 0;
}